package com.mini.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mini.conditon.BfDataCondition;
import com.mini.model.BusinessBigLessonOrder;
import com.mini.vo.*;
import com.mini.service.BusinessBigLessonOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bigLessonOrder")
@Api(tags = "大课订单相关接口-页面接口")
@Slf4j
public class BusinessBigOrderController extends BusinessControler{
    @Autowired
    private BusinessBigLessonOrderService bigLessonOrderService;


    @ApiOperation(value = "查询大课列表", notes = "大课订单列表")
    @GetMapping(value = "/order/list")
    public BusinessResult<List<BusinessBigLessonOrderVO>> bigOrderPage(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                       @ApiParam("机会id")@RequestParam Long chanceId){

        return BusinessResult.createSuccessInstance(bigLessonOrderService.bigOrderList(chanceId));
    }


    @ApiOperation(value = "查询大课订单日志", notes = "大课日志列表")
    @GetMapping(value = "/log/list")
    public BusinessResult<List<BusinessBigOrderLogVO>> bigOrderPage(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                    @ApiParam("大课订单号")@RequestParam String orderNo){

        return BusinessResult.createSuccessInstance(bigLessonOrderService.bigOrderLogByOrderNo(orderNo));
    }



    @ApiOperation(value = "从mini-api同步打开可订单", notes = "先同步到这里，判断能不能找到机会，如果有机会则保存")
    @PostMapping(value = "/order/syncFromApi")
    public BusinessResult syncBigOrderFromApi(@RequestHeader("sig") String sig,
                                              @RequestBody BusinessBigLessonOrder order
                                              ){
        log.info("大课订单同步数据：order={}", order);

        Boolean syncFlag = bigLessonOrderService.syncBigOrder(order);

        JSONObject json = new JSONObject();
        json.put("syncFlag", syncFlag);
        log.info("大课订单同步数据-结果：order={}，result={}", order, json);
        return BusinessResult.createSuccessInstance(json);
    }

    @ApiOperation(value = "根据咨询师id和时间统计大课订单")
    @PostMapping(value = "/order/countByTeacherId")
    public BusinessResult<List<BusinessBigOrderCountVO>> countBigOrder(@RequestHeader("sig") String sig,
                                                                       @RequestBody BusinessBigOrderCountParam param
                                                                       ){

        return BusinessResult.createSuccessInstance(bigLessonOrderService.countBigOrderByTeacherIds(param));
    }

    @ApiOperation(value = "根据时间统计大课订单")
    @GetMapping(value = "/order/countByDate")
    public BusinessResult<List<BusinessBigOrderCountVO>> countBigOrderByDay(@RequestHeader("sig") String sig,
                                                                       @ApiParam("开始时间 格式 yyyy-MM-dd hh:MM:ss")@RequestParam String startTime,
                                                                            @ApiParam("开始时间 格式 yyyy-MM-dd hh:MM:ss")@RequestParam String endTime
    ){

        return BusinessResult.createSuccessInstance(bigLessonOrderService.countBigOrderByDay(startTime, endTime));
    }

    @ApiOperation(value = "根据更新时间分页查询大课订单---服务内部调用")
    @PostMapping(value = "/order/page")
    public BusinessResult<PageResultVO<BfPackage>> page(@RequestHeader("sig") String sig,
                                                                      @RequestBody BfDataCondition bfDataCondition){

//        checkPageParams(bfDataCondition);
        bfDataCondition.setSortColumn("updateTime");
        return BusinessResult.createSuccessInstance(bigLessonOrderService.pageByUpdateTime(bfDataCondition));
    }


}
